package app.project.streetlensproject.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import app.project.streetlensproject.ListPlaceActivity;
import app.project.streetlensproject.MyApplication;
import app.project.streetlensproject.R;
import app.project.streetlensproject.util.CommonUtils;
import app.project.streetlensproject.util.Constants;

public class FgSetting extends Fragment {
	private CheckBox cbTypeDisplay;
	private View vTypeDisplay;
	private View vRadius;
	private TextView tvRadius;
	private View vSeparate1;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fg_setting, container, false);
		final int radius = CommonUtils.getIntSetting(MyApplication.getAppContext(), Constants.RADIUS, 5);
		final boolean isGetAll = CommonUtils.getBooleanSetting(MyApplication.getAppContext(), Constants.IS_DISPLAY_ALL, false);
		
		cbTypeDisplay = (CheckBox) v.findViewById(R.id.cb_type_display);
		vTypeDisplay = v.findViewById(R.id.v_type_display);
		vSeparate1 = v.findViewById(R.id.separate1);
		vRadius = v.findViewById(R.id.v_radius);
		tvRadius = (TextView) v.findViewById(R.id.tv_radius);
		
		tvRadius.setText(radius+" Km");
		cbTypeDisplay.setChecked(isGetAll);
		
		if(CommonUtils.getStringSetting(getActivity(), Constants.TOKEN_ID, null)!=null)
			setVisibleTypeDisplay(View.VISIBLE);
		else
			setVisibleTypeDisplay(View.GONE);
		
		initEvent();
		return v;
	}
	
	private void setVisibleTypeDisplay(int visibility){
		vTypeDisplay.setVisibility(visibility);
		vSeparate1.setVisibility(visibility);
	}

	private void initEvent() {
		vTypeDisplay.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				cbTypeDisplay.setChecked(!cbTypeDisplay.isChecked());
			}
		});
		vRadius.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
				builder.setTitle(R.string.choose_radius);

				final EditText input = new EditText(getActivity());
				input.setInputType(InputType.TYPE_CLASS_NUMBER);
				builder.setView(input);

				// Set up the buttons
				builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						String tmpStr = input.getText().toString();
						int numKm = 0;
						try {
							numKm = Integer.parseInt(tmpStr);
						} catch (NumberFormatException e) {
							e.printStackTrace();
							Toast.makeText(getActivity(), R.string.has_error, Toast.LENGTH_LONG).show();
							return;
						}
						if (numKm > 100) {
							numKm = 100;
							Toast.makeText(getActivity(), R.string.max_radius, Toast.LENGTH_LONG).show();
						}
						tvRadius.setText(numKm + " Km");
						if(CommonUtils.getIntSetting(MyApplication.getAppContext(), Constants.RADIUS, 5)==numKm)
							return;
						CommonUtils.saveIntSetting(MyApplication.getAppContext(), Constants.RADIUS, numKm);
						updateData();
					}
				});
				builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});

				builder.show();
			}
		});
		
		cbTypeDisplay.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton arg0, boolean value) {
				if(CommonUtils.getBooleanSetting(MyApplication.getAppContext(), Constants.IS_DISPLAY_ALL, false)==value)
					return;
				CommonUtils.saveBooleanSetting(MyApplication.getAppContext(), Constants.IS_DISPLAY_ALL, value);
				updateData();
			}
		});
	}

	private void updateData(){
		((ListPlaceActivity)getActivity()).updateData(true);
	}
}
